package com.platform.service.impl;

import com.platform.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.platform.dao.SpreadRankDao;
import com.platform.entity.SpreadRankEntity;
import com.platform.service.SpreadRankService;
import org.springframework.util.StringUtils;

/**
 * 排行榜Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-20 19:54:58
 */
@Service("tSpreadRankService")
public class TSpreadRankServiceImpl implements SpreadRankService {
    @Autowired
    private SpreadRankDao tSpreadRankDao;

    @Override
    public SpreadRankEntity queryObject(Long id) {
        return tSpreadRankDao.queryObject(id);
    }

    @Override
    public List<SpreadRankEntity> queryList(Map<String, Object> map) {
        return tSpreadRankDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return tSpreadRankDao.queryTotal(map);
    }

    @Override
    public int save(SpreadRankEntity tSpreadRank) {
        return tSpreadRankDao.save(tSpreadRank);
    }

    @Override
    public int update(SpreadRankEntity tSpreadRank) {
        return tSpreadRankDao.update(tSpreadRank);
    }

    @Override
    public int delete(Long id) {
        return tSpreadRankDao.delete(id);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        return tSpreadRankDao.deleteBatch(ids);
    }

    @Override
    public boolean initHistory(String month) {
        Date time = StringUtils.isEmpty(month) ? new Date():DateUtils.parseToDate(month, "yyyy-MM-dd")  ;

        String date = DateUtils.format(DateUtils.parseToDate(month, "yyyy-MM-dd"), "yyyy-MM");
        String beginDate = "2019-02-01";
        String endDate = DateUtils.monthLast(time);

        List<SpreadRankEntity> list = tSpreadRankDao.selectTeamList(beginDate, endDate, 0, 100);
        int i = 1;
        for (SpreadRankEntity t : list) {
            t.setMonthDate(date);
            t.setRanking(i);
            i++;
        }
        tSpreadRankDao.addSpreadRank(list);
        return true;
    }
}
